$(function () {
	//游戏名称
	window.gameName = 'plane-wars';
    //获得主界面
	var mainDiv=document.getElementById("gamePage");
    //初始化分数
	var scores=0;
	//获取屏幕尺寸
	var winHeight = $(window).height();
	var winWidth = $(window).width();
	//本方飞机
	var selfplan = null;
	//游戏计时器
	var gametimer = null;

	//判断用户是否有抽奖(游戏)资格
	api.eligibleToPlay();
	//获取活动参数
	api.getGameConfig(gameName);
	//添加底部链接
	setFootLink('#startPage');

	//点击[开始游戏]按钮
	$('#startBtn').on('click',function () {
		if (limitNum <= 0) {
			Toast('抽奖次数不够');
			return;
		}
		$('#startPage').hide();
		$('#gamePage').show();
		begin();
	});

	//点击再来一局
	$('.tryAgain').on('click',function () {
		$('#successPage,#failPage,#lotterySuccessBox,#lotteryFailBox').hide();
		$('#startPage,#successBox').show();
	});

	//创建飞机类
	function plan(hp,X,Y,sizeX,sizeY,score,dietime,sudu,boomimage,imagesrc){
	    this.planX=X;
	    this.planY=Y;
	    this.imagenode=null;
	    this.planhp=hp;
	    this.planscore=score;
	    this.plansizeX=sizeX;
	    this.plansizeY=sizeY;
	    this.planboomimage=boomimage;
	    this.planisdie=false;
	    this.plandietimes=0;
	    this.plandietime=dietime;
	    this.plansudu=sudu;
		//移动行为
	    this.planmove=function(){
            this.imagenode.style.top=this.imagenode.offsetTop+this.plansudu+"px";
	    }
	    this.init=function(){
	        this.imagenode=document.createElement("img");
	        this.imagenode.style.left=this.planX+"px";
	        this.imagenode.style.top=this.planY+"px";
	        this.imagenode.style.width=this.plansizeX+"px";
	        this.imagenode.style.height=this.plansizeY+"px";
	        this.imagenode.src=imagesrc;
	        mainDiv.appendChild(this.imagenode);
	    }
	    this.init();
	}

	//创建子弹类
	function bullet(X,Y,sizeX,sizeY,imagesrc){
	    this.bulletX=X;
	    this.bulletY=Y;
	    this.bulletimage=null;
	    this.bulletattach=1;
	    this.bulletsizeX=sizeX;
	    this.bulletsizeY=sizeY;
		//移动行为
	    this.bulletmove=function(){
	        this.bulletimage.style.top=this.bulletimage.offsetTop-20+"px";
	    }
	    this.init=function(){
	        this.bulletimage=document.createElement("img");
	        this.bulletimage.style.left= this.bulletX+"px";
	        this.bulletimage.style.top= this.bulletY+"px";
	        this.bulletimage.style.width=this.bulletsizeX+"px";
	        this.bulletimage.style.height=this.bulletsizeY+"px";
	        this.bulletimage.src=imagesrc;
	        mainDiv.appendChild(this.bulletimage);
	    }
	    this.init();
	}

	//创建单行子弹类
	function oddbullet(X,Y){
	    bullet.call(this,X,Y,10,20,"./img/bullet.png");
	}

	//创建敌机类
	function enemy(hp,a,b,sizeX,sizeY,score,dietime,sudu,boomimage,imagesrc){
	    plan.call(this,hp,random(a,b),-100,sizeX,sizeY,score,dietime,sudu,boomimage,imagesrc);
	}

	//产生min到max之间的随机数
	function random(min,max){
	    return Math.floor(min+Math.random()*(max-min));
	}

	//创建本方飞机类
	function ourplan(){
	    var imagesrc="./img/plane-my.png";
	    var boomimagsrc="./img/plane-my.png";
	    plan.call(this,1,'2.6rem','auto',92,71,0,660,0,boomimagsrc,imagesrc);
	    this.imagenode.setAttribute('id','ourplan');
	    this.imagenode.style.left = winWidth*.42 + 'px';
	    this.imagenode.style.top = winHeight*.8 + 'px';
	}

	

	//给我方飞机绑定拖动事件
	$('#gamePage').on('touchstart','#ourplan',function(e){
		e.preventDefault();
		var elm = $(this).position();
		var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
		box_x = touch.pageX - elm.left;
		box_y = touch.pageY - elm.top;
	});

	$('#gamePage').on('touchmove','#ourplan',function(e){
		e.preventDefault();
		var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
		$(this).css('left', touch.pageX - box_x + 'px');
		$(this).css('top', touch.pageY - box_y + 'px'); 
	});
	
	//敌机对象数组
	var enemys=[];

	//子弹对象数组
	var bullets=[];
	var mark=0;
	var mark1=0;
	
	//开始函数
	function start(){
	    mark++;

	    //创建敌方飞机
	    if(mark==20){
	        mark1++;
	        //中飞机
	        if(mark1%5==0){
	            enemys.push(new enemy(10,25,274,71,62,10,360,random(1,3),"./img/bomb.png","./img/plane-m.png"));
	        }
	        //大飞机
	        if(mark1==20){
	            enemys.push(new enemy(20,57,210,92,78,20,540,1,"./img/bomb.png","./img/plane-l.png"));
	            mark1=0;
	        }
	        //小飞机
	        else{
	            enemys.push(new enemy(1,19,286,50,41,1,360,random(1,4),"./img/bomb.png","./img/plane-s.png"));
	        }
	        mark=0;
	    }

		//移动敌方飞机
	    var enemyslen=enemys.length;
	    for(var i=0;i<enemyslen;i++){
	        if(enemys[i].planisdie!=true){
	            enemys[i].planmove();
	        }
			//如果敌机超出边界,删除敌机
	        if(enemys[i].imagenode.offsetTop > winHeight){
	            mainDiv.removeChild(enemys[i].imagenode);
	            enemys.splice(i,1);
	            enemyslen--;
	        }
	        //当敌机死亡标记为true时，经过一段时间后清除敌机
	        if(enemys[i].planisdie==true){
	            enemys[i].plandietimes+=20;
	            if(enemys[i].plandietimes==enemys[i].plandietime){
	                mainDiv.removeChild(enemys[i].imagenode);
	                enemys.splice(i,1);
	                enemyslen--;
	            }
	        }
	    }

		//创建子弹
	    if(mark%5==0){
            bullets.push(new oddbullet(parseInt(selfplan.imagenode.style.left)+45,parseInt(selfplan.imagenode.style.top)-15));
	    }

		//移动子弹
	    var bulletslen=bullets.length;
	    for(var i=0;i<bulletslen;i++){
	        bullets[i].bulletmove();
			//如果子弹超出边界,删除子弹
	        if(bullets[i].bulletimage.offsetTop<0){
	            mainDiv.removeChild(bullets[i].bulletimage);
	            bullets.splice(i,1);
	            bulletslen--;
	        }
	    }

		//碰撞判断
	    for(var k=0;k<bulletslen;k++){
	        for(var j=0;j<enemyslen;j++){
	            //判断碰撞本方飞机
	            if(enemys[j].planisdie==false){
	                if(enemys[j].imagenode.offsetLeft+enemys[j].plansizeX>=selfplan.imagenode.offsetLeft&&enemys[j].imagenode.offsetLeft<=selfplan.imagenode.offsetLeft+selfplan.plansizeX){
	                  if(enemys[j].imagenode.offsetTop+enemys[j].plansizeY>=selfplan.imagenode.offsetTop+40&&enemys[j].imagenode.offsetTop<=selfplan.imagenode.offsetTop-20+selfplan.plansizeY){
	                      //碰撞本方飞机，游戏结束，统计分数
	                      selfplan.imagenode.src="./img/bomb.png";
	                      clearInterval(gametimer);
	                      selfplan = null;
	                      if (scores >= 100) {
	                      	$('#successScore').text(scores+'分');
	                      	$('#successPage').show();
	                      } else {
	                      	$('#failScore').text(scores+'分');
	                      	$('#failPage').show();
	                      }
	                      return;
	                  }
	                }
	                //判断子弹与敌机碰撞
	                if((bullets[k].bulletimage.offsetLeft+bullets[k].bulletsizeX>enemys[j].imagenode.offsetLeft)&&(bullets[k].bulletimage.offsetLeft<enemys[j].imagenode.offsetLeft+enemys[j].plansizeX)){
	                    if(bullets[k].bulletimage.offsetTop<=enemys[j].imagenode.offsetTop+enemys[j].plansizeY&&bullets[k].bulletimage.offsetTop+bullets[k].bulletsizeY>=enemys[j].imagenode.offsetTop){
	                        //敌机血量减子弹攻击力
	                        enemys[j].planhp=enemys[j].planhp-bullets[k].bulletattach;
	                        //敌机血量为0，敌机图片换为爆炸图片，死亡标记为true，计分
	                        if(enemys[j].planhp==0){
	                            scores=scores+enemys[j].planscore;
	                            $('#counter').text(scores);
	                            enemys[j].imagenode.src=enemys[j].planboomimage;
	                            enemys[j].planisdie=true;
	                        }
	                        //删除子弹
	                        mainDiv.removeChild(bullets[k].bulletimage);
                            bullets.splice(k,1);
                            bulletslen--;
                            break;
	                    }
	                }
	            }
	        }
	    }
	}
	//开始游戏
	function begin(){
		limitNum--;
		$('#successPage,#failPage').hide();
        $('#gamePage').html('<div id="counter">0</div>');
    	selfplan = new ourplan();
		enemys=[];
		bullets=[];
		mark=0;
		mark1=0;
		scores=0;
    	gametimer = setInterval(start,20);
	}
});
